# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=dulwich
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.22.8
pkgrel=1
pkgdesc="Python Git Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.dulwich.io/'
msys2_repository_url='https://github.com/jelmer/dulwich'
msys2_references=(
  'archlinux: python-dulwich'
  'purl: pkg:pypi/dulwich'
)
license=('spdx:Apache-2.0 OR spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-urllib3")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools-rust"
             "${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('!strip')
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz")
sha256sums=('701547310415de300269331abe29cb5717aa1ea377af826bf513d0adfb1c209b')

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/python -m pytest
}

_opt="$(shopt -p extglob)"
shopt -s extglob
package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  local _opt="$(shopt -p extglob)"
  shopt -s extglob
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/!(*.exe); do
    sed -e '1 { s/^#!.*python.*$/#!\/usr\/bin\/env python3/ }' -i ${_f}
  done
  eval "${_opt}"

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
eval "${_opt}"
unset _opt
